package com.starrocks.log;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

/**
 * 自定义 Logback Appender
 * 拦截日志并发送到 LogCollector
 */
public class LogCollectorAppender extends AppenderBase<ILoggingEvent> {
    
    private static LogCollector logCollector;
    
    /**
     * 设置 LogCollector（由 Spring 注入）
     */
    public static void setLogCollector(LogCollector collector) {
        logCollector = collector;
    }
    
    @Override
    protected void append(ILoggingEvent event) {
        if (logCollector != null) {
            String level = event.getLevel().toString();
            String logger = event.getLoggerName();
            String message = event.getFormattedMessage();
            String thread = event.getThreadName();
            
            // 异步发送，避免阻塞日志输出
            try {
                logCollector.addLog(level, logger, message, thread);
            } catch (Exception e) {
                // 忽略异常，避免影响正常日志输出
            }
        }
    }
}

